package com.batchaps.classes;

import javax.microedition.lcdui.Graphics;

public class MyPlayer {
    private Sprite2D player, death;
    private Sprite2D[] Bullet1;
    private Sprite2D[] Bullet2;
    private Sprite2D[] Bullet3;
    private Sprite2D[] Bomb;
    private int[] isShoot1;
    private int[] isShoot2;
    private int[] isShoot3;
    private int[] Bullet3MoveTime;
    private int[] isBomb;
    private int iwidth, iheight, iX, iY, timeFire1, timeFire2;
    private int health, power, timewait, nBullet1, nBullet2, nBullet3, nBomb;
    private int timeInvicible;
    private int bomb;
    //private Sound sound;
    public MyPlayer(Sprite2D player, int width, int height)
    {
        try
        {
            this.player = player;
            player.set_posX(50);
            player.set_posY(50);
            iwidth = width;
            iheight = height;
            nBullet1 = 12;
            nBullet2 = 12;
            nBullet3 = 10;
            nBomb = 18;
            bomb = 50;
            Bullet1 = new Sprite2D[nBullet1];
            isShoot1 = new int[nBullet1];
            Bullet2 = new Sprite2D[nBullet2];
            isShoot2 = new int[nBullet2];
            Bullet3 = new Sprite2D[nBullet3];
            isShoot3 = new int[nBullet3];
            Bullet3MoveTime = new int[nBullet3];
            Bomb = new Sprite2D[nBomb];
            isBomb = new int[nBomb];
            health = 10;
            power = 0;
            timeFire1 = 0;
            timeFire2 = 0;
            timewait = 0;
            //sound = s;
            timeInvicible = 0;
            for (int i = 0; i < nBullet1; i++)
            {
                try
                {
                    Sprite2D bullet = new Sprite2D("bulletSmall" , 5 , ".png");
                    Bullet1[i] = bullet;
                    Bullet1[i].set_visible(false);
                    isShoot1[i] = 0;
                }
                catch (Exception ex)
                {
                }
            }
            death = new Sprite2D("DevildomTorchOrrerriesSun",5 , ".png" , 32, 32, 0 , 0, 0, 0);
            death.SetFrame(2);
            death.set_visible(false);
            for (int i = 0; i < nBullet2; i++)
            {
                try
                {
                    Sprite2D bullet = new Sprite2D("Fireballs" ,5 , ".png" ,32, 32 , 0 , 0, 0, 0);
                    Bullet2[i] = bullet;
                    Bullet2[i].SetFrame(0);
//                    int[] frameSequence = { 2,5,8,11,14,1,3,5 };
//                    Bullet2[i].setFrameSequence(frameSequence);
                    Bullet2[i].set_visible(false);
                    //Bullet2[i].setTransform(Bullet2[i].TRANS_ROT90);
                    isShoot2[i] = 0;
                }
                catch (Exception ex)
                {
                }
            }
            for (int i = 0; i < nBullet3; i++)
            {
                try
                {
                    Sprite2D bullet = new Sprite2D("Fireballs" , 5 , ".png", 32, 32 , 0 , 0 ,0 ,0);
                    Bullet3[i] = bullet;
                    Bullet3[i].SetFrame(8);
//                    int[] frameSequence = { 0,3,6,9,10,12,13 };
//                    Bullet3[i].setFrameSequence(frameSequence);
                    Bullet3[i].set_visible(false);
                    //Bullet3[i].setTransform(Bullet3[i].TRANS_ROT90);
                    //lm.append(Bullet3[i]);
                    isShoot3[i] = 0;
                    Bullet3MoveTime[i] = 10;
                }
                catch (Exception ex)
                {
                    //ex.printStackTrace();
                }
            }
            for (int i = 0; i < nBomb; i++)
            {
                try
                {
                    Sprite2D bombTemp = new Sprite2D("bom" , 5 , ".png", 32, 32 , 0 , 0 , 0 , 0);
                    Bomb[i] = bombTemp;
//                    int[] frameSequence = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1 };
//                    Bomb[i].setFrameSequence(frameSequence);
                    //Bomb[i].defineReferencePixel(Bomb[i].getWidth() / 2, Bomb[i].getHeight() / 2);
                    Bomb[i].set_visible(false);
                    //Bomb[i].setTransform(Bomb[i].TRANS_ROT180);
                    //lm.append(Bomb[i]);
                    isBomb[i] = 0;
                }
                catch (Exception ex)
                {
                    //ex.printStackTrace();
                }
            }
            iX = (int)player.getX();
            iY = (int)player.getY();
        }
        catch (Exception ex)
        {
            //ex.printStackTrace();
        }
    }
    public void Move(int x, int y)
    {
        player.Move(x, y);
    }
    public int getX()
    {
        return (int)player.getX();
    }
    public int getY()
    {
        return (int)player.getY();
    }
    public void Draw(Graphics g)
    {
        player.Draw(g);
        death.Draw(g);
        for (int i = 0; i < nBullet1; i++)
            Bullet1[i].Draw(g);
        for (int i = 0; i < nBullet2; i++)
            Bullet2[i].Draw(g);
        for (int i = 0; i < nBullet3; i++)
            Bullet3[i].Draw(g);
        for (int i = 0; i < nBomb; i++)
            Bomb[i].Draw(g);
    }
    //public void moveLeft()
    //{
    //    if (player.getX() - 1 > 10)
    //    {
    //        player.Move(-3, 0);
    //    }
    //}
    //public void moveRight()
    //{
    //    if (player.getX() + 1 < iwidth - 10)
    //    {
    //        player.Move(3, 0);
    //    }
    //}
    //public void moveUp(int iY)
    //{
    //    if (player.getY() - 1 > iY + 10)
    //    {
    //        player.Move(0, -3);
    //    }
    //}
    //public void moveDown(int iY)
    //{
    //    if (player.getY() + 1 < iY + iheight - 10)
    //    {
    //        player.Move(0, 3);
    //    }
    //}
    //public void moveLeftUp(int iY)
    //{
    //    if (player.getRefPixelY() - 1 > iY + 10 && player.getRefPixelX() - 1 > 10)
    //    {
    //        player.move(-3, -3);
    //    }
    //}
    //public void moveLeftDown(int iY)
    //{
    //    if (player.getRefPixelY() + 1 < iY + iheight - 10 && player.getRefPixelX() - 1 > 10)
    //    {
    //        player.move(-3, 3);
    //    }
    //}
    //public void moveRightUp(int iY)
    //{
    //    if (player.getRefPixelY() - 1 > iY + 10 && player.getRefPixelX() + 1 < iwidth - 10)
    //    {
    //        player.move(3, -3);
    //    }
    //}
    //public void moveRightDown(int iY)
    //{
    //    if (player.getRefPixelY() + 1 < iY + iheight - 10 && player.getRefPixelX() + 1 < iwidth - 10)
    //    {
    //        player.move(3, 3);
    //    }
    //}
    public void PlayerFire(int scrY)
    {
        timeFire1--;
        timeFire2--;
        iX = (int)player.getX();
        iY = (int)player.getY();
        for (int i = 0; i < nBullet1; i++)
        {
            if (Bullet1[i].getY() < 0)
            {
                Bullet1[i].set_visible(false);
                isShoot1[i] = 0;
            }
            if (Bullet2[i].getY() < 0)
            {
                Bullet2[i].set_visible(false);
                isShoot2[i] = 0;
            }
        }
        for (int i = 0; i < nBullet3; i++)
        {
            if (Bullet3[i].getY() < 0)
            {
                Bullet3[i].set_visible(false);
                isShoot3[i] = 0;
            }
        }
        for (int i = 0; i < nBullet1; i++)
        {
            if (isShoot1[i] != 0)
            {
                Bullet1[i].Move(0, -10);
            }
            if (isShoot2[i] != 0)
            {
                Bullet2[i].Move(0, -10);
                Bullet2[i].NextFrame();
            }
        }
        for (int i = 0; i < nBullet3; i++)
        {
            if (isShoot3[i] != 0)
            {
                switch (isShoot3[i])
                {
                    case 1:
                        {
                            if (Bullet3MoveTime[i] > 0)
                            {
                                Bullet3[i].Move(-4, 0);
                                Bullet3MoveTime[i]--;
                            }
                            else
                            {
                                Bullet3[i].Move(0, -8);
                            }
                        } break;
                    case 2:
                        {
                            if (Bullet3MoveTime[i] > 0)
                            {
                                Bullet3[i].Move(4, 0);
                                Bullet3MoveTime[i]--;
                            }
                            else
                            {
                                Bullet3[i].Move(0, -8);
                            }
                        } break;
                }
                Bullet3[i].NextFrame();
            }
        }
        if (power < 10)
        {
            if (timeFire1 <= 0)
            {
                for (int i = 0; i < nBullet1; i++)
                {
                    if (isShoot1[i] == 0)
                    {
                        Bullet1[i].setPosition(iX - player.get_width() /2, iY);
                        Bullet1[i].set_visible(true);
                        isShoot1[i] = 1;
                        break;
                    }
                }
                for (int i = 0; i < nBullet1; i++)
                {
                    if (isShoot1[i] == 0)
                    {
                        Bullet1[i].setPosition(iX + player.get_width()  / 2, iY);
                        Bullet1[i].set_visible(true);
                        isShoot1[i] = 1;
                        break;
                    }
                }
                //sound.playerFireSound();
                timeFire1 = 15;
            }
        }
        if (power >= 10)
        {
            if (timeFire1 <= 0)
            {
                for (int i = 0; i < nBullet2; i++)
                {
                    if (isShoot2[i] == 0)
                    {
                        Bullet2[i].setPosition(iX - player.get_width()  / 2, iY);
                        Bullet2[i].set_visible(true);
                        isShoot2[i] = 1;
                        break;
                    }
                }
                for (int i = 0; i < nBullet2; i++)
                {
                    if (isShoot2[i] == 0)
                    {
                        Bullet2[i].setPosition(iX + player.get_width() / 2, iY);
                        Bullet2[i].set_visible(true);
                        isShoot2[i] = 1;
                        break;
                    }
                }
                //sound.playerFireSound();
                timeFire1 = 15;
            }
        }
        if (power >= 20 && timeFire2 <= 0)
        {
            for (int i = 0; i < nBullet3; i++)
            {
                if (isShoot3[i] == 0)
                {
                    Bullet3[i].setPosition(iX, iY);
                    Bullet3[i].set_visible(true);
                    isShoot3[i] = 1;
                    Bullet3MoveTime[i] = 6;
                    break;
                }
            }
            for (int i = 0; i < nBullet3; i++)
            {
                if (isShoot3[i] == 0)
                {
                    Bullet3[i].setPosition(iX, iY);
                    Bullet3[i].set_visible(true);
                    isShoot3[i] = 2;
                    Bullet3MoveTime[i] = 6;
                    break;
                }
            }
            timeFire2 = 20;
        }
    }
    public void useBomb(int scrY)
    {
        if (bomb > 0)
        {
            int pos = iheight + Bomb[0].get_height() / 2;
            Bomb[0].setPosition(10, 0 + pos);
            Bomb[0].set_visible(true);
            Bomb[1].setPosition(iwidth / 2, 0 + pos);
            Bomb[1].set_visible(true);
            Bomb[2].setPosition(iwidth - 10, 0 + pos);
            Bomb[2].set_visible(true);
            int j = 3;
            for (int i = 0; i < 3; i++)
            {
                Bomb[j].setPosition(iwidth / 4, 0 + pos + 30);
                Bomb[j].set_visible(true);
                Bomb[j + 1].setPosition(iwidth - iwidth / 4, 0 + pos + 30);
                Bomb[j + 1].set_visible(true);
                Bomb[j + 2].setPosition(10, 0 + pos + 60);
                Bomb[j + 2].set_visible(true);
                Bomb[j + 3].setPosition(iwidth / 2, 0 + pos + 60);
                Bomb[j + 3].set_visible(true);
                Bomb[j + 4].setPosition(iwidth - 10, 0 + pos + 60);
                Bomb[j + 4].set_visible(true);
                j += 5;
                pos += 60;
            }
            for (int i = 0; i < nBomb; i++)
            {
                isBomb[i] = 1;
            }
            bomb--;
            //sound.playerBombSound();
        }
    }
    public void moveBomb(int scrY)
    {
        for (int i = 0; i < nBomb; i++)
        {
            if (Bomb[i].getY() < 0)
            {
                Bomb[i].set_visible(false);
            }
        }
        for (int i = 0; i < nBomb; i++)
        {
            if (isBomb[i] != 0)
            {
                Bomb[i].Move(0, -3);
                Bomb[i].NextFrame();
            }
        }
    }
    public int getHealth()
    {
        return health;
    }
    public int getBomb()
    {
        return bomb;
    }
    public void refreshPlayer()
    {
        health = 10;
        power = 0;
        bomb = 10;
        death.set_visible(false);
        player.setPosition(200, 700);
        for (int i = 0; i < nBullet1; i++)
        {
            Bullet1[i].set_visible(false);
        }
        for (int i = 0; i < nBullet2; i++)
        {
            Bullet2[i].set_visible(false);
        }
        for (int i = 0; i < nBullet3; i++)
        {
            Bullet3[i].set_visible(false);
        }
        for (int i = 0; i < nBomb; i++)
        {
            Bomb[i].set_visible(false);
        }
    }
    public void setHealth(int health)
    {
        if (timeInvicible <= 0)
        {
            player.set_visible(true);
            if (this.health > health)
            {
                player.set_visible(false);
                this.health = health;
                if (power - 3 >= 0)
                    power -= 3;
                else
                    power = 0;
                timeInvicible = 150;
            }
        }
        else
        {
            if (timeInvicible % 2 == 0)
            {
                player.set_visible(false);
            }
            else
            {
                player.set_visible(true);
            }
        }
    }
    public int getPower()
    {
        return power;
    }
    public void setPower(int power)
    {
        if (power <= 70)
            this.power = power;
    }
    public int checkFire(Sprite2D sprite, int health)
    {
        for (int i = 0; i < nBullet1; i++)
        {
            if (Bullet1[i].CollidesWith(sprite))
            {
                Bullet1[i].set_visible(false);
                isShoot1[i] = 0;
                health--;
            }
            if (Bullet2[i].CollidesWith(sprite))
            {
                Bullet2[i].set_visible(false);
                isShoot2[i] = 0;
                health--;
            }
        }
        for (int i = 0; i < nBullet3; i++)
        {
            if (Bullet3[i].CollidesWith(sprite))
            {
                Bullet3[i].set_visible(false);
                isShoot3[i] = 0;
                health -= 5;
            }
        }
        for (int i = 0; i < nBomb; i++)
        {
            if (Bomb[i].CollidesWith(sprite))
            {
                health -= 5;
            }
        }
        return health;
    }
    public void playerPositon()
    {
        player.Move(0, -1);
    }
    public void playerFrame()
    {
        if (timewait <= 0)
        {
            player.NextFrame();
            timewait = 4;
        }
        timewait--;
        timeInvicible--;
    }
    public boolean checkCondition()
    {
        int iX = (int)player.getX();
        int iY = (int)player.getY();
        boolean result = false;
        if (health <= 0)
        {
            player.set_visible(false);
            death.setPosition(iX, iY);
            death.set_visible(true);
            //sound.enemyDeathSound();
            result = true;
        }
        return result;
    }
}

